using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class MenuComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        SpriteBatch _spriteBatch;
        SpriteFont _menuFont;
        List<Button> _listItemMenu;
        public List<Button> ListItemMenu
        {
            get { return _listItemMenu; }
            set { _listItemMenu = value; }
        }


        //Initilation
        public MenuComponent(Game game, SpriteFont spriteFont, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _menuFont = spriteFont;
            _spriteBatch = spriteBatch;

            //Init list menu item
            ListItemMenu = new List<Button>();
            ListItemMenu.Add(new Button("Start", _menuFont, new Vector2(480, 200), Color.Red)) ;
            ListItemMenu.Add(new Button("Setting", _menuFont, new Vector2(450, 290), Color.Red));
            ListItemMenu.Add(new Button("About", _menuFont, new Vector2(470, 380), Color.Red));
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            /*TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                touchPosition = touchCollection[0].Position;
                pretouchPosition = touchPosition;
            }
            for (int i = 0; i < ListItemMenu.Count; i++)
                ListItemMenu[i].ButtonIsTouched(touchPosition, i);*/
            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    for (int i = 0; i < ListItemMenu.Count; i++)
                        ListItemMenu[i].ButtonIsTouched(gesture.Position, i);
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            for (int i = 0; i < ListItemMenu.Count; i++)
                ListItemMenu[i].Draw(_spriteBatch, gameTime);
        }
    }
}
